<template>
  <!-- <PermissionContainer
    :jiBieArr="[2]"
    :tipArr="[
      ,
      '申请退款只对站点级别开放',
      '因为集团上关于申请退款的权限配置，此级没有申请退款权限',
      ,
      '您暂未启动退款功能',
    ]"
  > -->
  <PermissionContainer>
    <div class="app-container">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
        class="globalSerchFormStyle"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="订单号" prop="orderNo">
              <el-input
                v-model="queryParams.orderNo"
                placeholder="请输入订单号"
                clearable
                size="small"
                style="width: 190px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="客户姓名" prop="likeConsumer">
              <el-input
                v-model="queryParams.likeConsumer"
                placeholder="请输入客户姓名"
                clearable
                size="small"
                style="width: 190px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="油品名称" prop="oilName">
              <el-select
                v-model="queryParams.oilName"
                placeholder="油品名称"
                clearable
                size="small"
                style="width: 190px"
              >
                <el-option
                  v-for="dict in oilNameOptions"
                  :key="dict.dictLabel"
                  :label="dict.dictLabel"
                  :value="dict.dictLabel"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="下单时间" prop="createdDate">
              <el-date-picker
                style="width: 190px"
                v-model="dateRangeCreatedDate"
                size="small"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-row class="global_el_rowStyle" :gutter="10">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-refresh"
            size="mini"
            @click="resetQuery"
            plain
            >重置</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="info"
            size="mini"
            style="margin-center: 10px"
            id="closeSearchBtn"
            @click="
              (closeSearch) => {
                collapse = !collapse;
              }
            "
          >
            {{ collapse ? "收起" : "展开" }}
            <i
              :class="collapse ? 'el-icon-arrow-up ' : 'el-icon-arrow-down'"
            ></i>
          </el-button>
        </el-col> -->
      </el-row>
      <el-table :data="orderList"  stripe>
        <af-table-column label="序号" width="80" sortable>
          <template slot-scope="scope">
            {{
              (queryParams.pageNum - 1) * queryParams.pageSize +
              scope.$index +
              1
            }}
          </template>
        </af-table-column>
        <af-table-column
          label="油站名"
          align="left"
          prop="stationName"
          v-if="jiBie == 0 || jiBie == 1"
          sortable
        />
        <af-table-column
          v-if="false"
          label="订单id"
          align="left"
          prop="orderId"
          sortable
        />
        <af-table-column label="订单号" align="left" prop="orderNo" sortable />
        <af-table-column
          label="油品名称"
          align="left"
          width="100"
          prop="oilName"
          sortable
        />
        <af-table-column
          label="油品价格(元)"
          align="left"
          prop="oilPirce"
          sortable
        />
        <af-table-column
          label="数量(L/kg)"
          align="left"
          prop="orderLiters"
          sortable
        />
        <af-table-column
          label="应付金额(元)"
          align="left"
          prop="receivableAmt"
          sortable
        />
        <af-table-column
          label="实付金额(元)"
          align="left"
          prop="amt"
          sortable
        />
        <af-table-column
          label="立减优惠金额(元)"
          align="left"
          prop="discountAmt"
          sortable
        />
        <af-table-column
          label="优惠券金额(元)"
          align="left"
          prop="discountCouponAmt"
          sortable
        />
        <af-table-column
          label="客户姓名"
          align="left"
          width="120"
          prop="consumer"
          sortable
        />
        <af-table-column
          label="加油员"
          align="left"
          prop="oilPersonnel"
          sortable
        />
        <af-table-column
          label="加油枪号"
          align="left"
          width="120"
          prop="oilGun"
          sortable
        />
        <af-table-column
          label="支付类型"
          align="left"
          prop="payTypeName"
          width="150"
          sortable
        />
        <af-table-column
          label="油站名称"
          align="left"
          v-if="false"
          prop="stationName"
          sortable
        />
        <af-table-column
          v-if="false"
          label="用户id(消费者)"
          align="left"
          prop="consumerId"
          sortable
        />
        <af-table-column
          v-if="false"
          label="油站id"
          align="left"
          prop="stationId"
          sortable
        />
        <af-table-column
          label="支付状态"
          width="130"
          align="left"
          prop="status"
          :formatter="statusFotmat"
          sortable
        />
        <af-table-column label="支付时间" align="left" prop="payDate" sortable>
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.payDate, "{y}-{m}-{d} {h}:{i}:{s}")
            }}</span>
          </template>
        </af-table-column>
        <af-table-column
          label="创建时间"
          align="left"
          prop="createdDate"
          sortable
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.createdDate, "{y}-{m}-{d} {h}:{i}:{s}")
            }}</span>
          </template>
        </af-table-column>
        <af-table-column
          show-overflow-tooltip
          label="状态"
          align="left"
          class-name="small-padding fixed-width"
          width="120"
          sortable
        >
          <template slot-scope="scope">
            {{ parseExaminStatus(scope.row.examinStatus) }}
          </template>
        </af-table-column>

        <af-table-column
          label="操作"
          align="left"
          fixed="right"
          class-name="small-padding fixed-width"
          width="120"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-plus"
              :disable="scope.row.examinStatus == '2'"
              v-if="scope.row.status == 1"
              @click="startRefund(scope.row)"
              >{{
                scope.row.examinStatus == null
                  ? "申请退款"
                  : "" + scope.row.examinStatus == "0"
                  ? "申请退款"
                  : "" + scope.row.examinStatus == "1"
                  ? "撤销退款申请"
                  : "" + scope.row.examinStatus == "3"
                  ? "再次发起申请"
                  : "" + scope.row.examinStatus == "2"
                  ? "已经退款"
                  : "" + scope.row.examinStatus == "4"
                  ? "再次发起申请"
                  : ""
              }}</el-button
            >
          </template>
        </af-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </PermissionContainer>
</template>
<script>
import { listOrder, exportOrder, printOrderInfo } from "@/api/station/order";
import { listPrice, getPrice } from "@/api/station/price";
import { listManage } from "@/api/station/manage";
import { applyExaminRefund, recallApplyRefund } from "@/api/order";

export default {
  name: "Order_Oil",
  data() {
    return {
      collapse: false,
      //创建订单时间间隔
      dateRangeCreatedDate: [],
      // 总条数
      total: 0,
      // 订单支付表格数据
      orderList: [],
      oilNameOptions: [],
      // 字典
      payTypeOptions: [],
      // 下拉油站
      stationOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        levelId: this.levelId,
        pageNum: 1,
        pageSize: 10,
        oilGun: null,
        oilName: null,
        stationId: null,
        orderType: null,
        oilPersonnel: null,
        createdDate: null,
        likeConsumer: null,
        orderNo: null,
      },
      // 表单校验
      rules: {},
      reprint: false,
    };
  },
  created() {
    this.getList();
    listManage({
      pageNum: 1,
      pageSize: 1,
    }).then((response) => {
      const equipmentList = response.rows;
      if (!!equipmentList) {
        if ((equipmentList[0] || {}).deviceType == "1") {
          this.reprint = true;
        }
      }
    });
    //字典
    this.getDicts("pay_type").then((response) => {
      this.payTypeOptions = response.data;
    });
    this.getDicts("oil_name").then((response) => {
      this.oilNameOptions = response.data;
    });
  },
  methods: {
    parseExaminStatus(status) {
      if (status == null || status == "0") {
        return "未申请";
      } else if (status == "1") {
        return "已申请退款，等待机构审核";
      } else if (status == "2") {
        return "退款成功";
      } else if (status == "3") {
        return "机构已驳回退款，失败原因联系审核机构";
      } else if (status == "4") {
        return "退款失败，失败原因联系审核机构";
      }
    },
    startRefund(row) {
      if (row.examinStatus == "1") {
        recallApplyRefund(row)
          .then((res) => {
            this.msgSuccess("撤回退款成功");
          })
          .then((res) => {
            this.getList();
          });
      } else if (row.examinStatus == null || row.examinStatus == "0") {
        applyExaminRefund(row)
          .then((res) => {
            this.msgSuccess("已申请退款");
          })
          .then((res) => {
            this.getList();
          });
      } else if (row.examinStatus == 4) {
        applyExaminRefund(row)
          .then((res) => {
            this.msgSuccess("再次申请成功");
          })
          .then((res) => {
            this.getList();
          });
      } else if (row.examinStatus == "2") {
        console.log("已经退款成功");
      } else if (row.examinStatus == "3") {
        applyExaminRefund(row)
          .then((res) => {
            this.msgSuccess("已申请退款");
          })
          .then((res) => {
            this.getList();
          });
      }
    },
    payTypeFotmat(row, column) {
      // if (row.payType === "wx") {
      //   return "微信支付";
      // } else if (row.payType === "zfb") {
      //   return "支付宝";
      // } else if (row.payType === "xj") {
      //   return "现金支付";
      // } else if (row.payType === "dzk") {
      //   return "电子卡支付";
      // } else if (row.payType === "POS") {
      //   return "POS机支付";
      // } else if (row.payType === "yzf") {
      //   return "预支付";
      // } else if (row.payType === "kbzf") {
      //   return "卡包支付";
      // }
      const types = {
        wx: "微信支付",
        zfb: "支付宝支付",
        xj: "现金支付",
        dzk: "电子卡支付",
        pos: "POS机支付",
        yzf: "预支付",
        kbzf: "卡包支付",
        cdk: "车队卡支付",
      };
      return types[row.payType] ?? "";
    },
    statusFotmat(row, column) {
      if (row.status === "0") {
        return "未支付";
      } else if (row.status === "1") {
        return "已支付";
      } else if (row.status === "2") {
        return "线下退款";
      } else if (row.status === "3") {
        return "线上退款";
      }
    },
    /** 查询订单支付列表 */
    getList() {
      this.queryParams.levelId = this.levelId;
      listOrder(
        this.addDateRange(this.queryParams, this.dateRangeCreatedDate)
      ).then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreatedDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //打印订单小票
    printOrderInfo(row) {
      printOrderInfo({ orderId: row.orderId }).then((response) => {
        this.msgSuccess("小票打印成功");
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有订单支付数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportOrder(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
